package com.hmall.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 不带参数
 * @author xiaopeng
 * @version 1.0
 */
@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    //如果需要考虑不同自定义过滤器的优先级，需要使用GateWayFilter的装饰类OrderedGatewayFilter
    @Override
    public GatewayFilter apply(Object config) {
        // OrderedGatewayFilter是GatewayFilter的子类，包含两个参数：
        // - GatewayFilter：过滤器
        // - int order值：值越小，过滤器执行优先级越高
        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                //1.获取请求
                ServerHttpRequest request = exchange.getRequest();
                //2.过滤器业务处理
                System.out.println("PrintAnyGatewayFilter pre 阶段执行了！");
                System.out.println("request head:" + request.getHeaders());
                // 放行
                return chain.filter(exchange);
            }
        }, 3);
    }


}
